using UnityEngine;
using System.Collections;
using ProtoBuf;

public class SampleSerializer : MonoBehaviour {
	
	[ProtoContract]
	public enum eSampleEnum
	{
		Enum1,
		Enum2,
		Enum3,
	}
	
	[System.Serializable]
	[ProtoContract]
	public class CharacterData
	{
		[ProtoContract]
		public struct ItemData
		{
			[ProtoMember(1)]
			public string itemName;
			[ProtoMember(2)]
			public int id;
			[ProtoMember(3)]
			public System.Collections.Generic.List<string> listStr;
		}
		
		[ProtoMember(1)]
		public string name = "";
		[ProtoMember(2)]
		public int score = 0;
		[ProtoMember(3)]
		public ItemData[] items = null;
		[ProtoMember(4)]
		public eSampleEnum sampleEnum;
	}

	public CharacterData mData;
	static string mDataLocation = Application.persistentDataPath + "/Data";

	void Awake()
	{

#if UNITY_EDITOR
		//use to initialize and rebuild the serializer. only once in application runtime 
		//NOTE:use only this for editor
		SerializeUtility.Init( new System.Type[]{ typeof(CharacterData)} );
#endif

		//load data if exist
		if( SerializeUtility.IsFileExist( mDataLocation))
		{
			mData = (CharacterData)SerializeUtility.LoadObject( mDataLocation, typeof(CharacterData));
			Debug.Log( "SAMPLE: Data loaded from " + mDataLocation);
		}
	}

	void OnGUI()
	{
		//use to save the data on the specified location
		if(GUI.Button(new Rect(0, 0, 100, 100), "buttonName"))
		{
			SerializeUtility.SaveObject( mDataLocation, mData);
			Debug.Log( "SAMPLE: Dave saved to " + mDataLocation);
		}

		if(GUI.Button(new Rect(100, 0, 100, 100), "LoadObject"))
		{
			if(SerializeUtility.IsFileExist(mDataLocation))
			{
				mData = (CharacterData)SerializeUtility.LoadObject(mDataLocation, typeof(CharacterData));
				Debug.Log("Data loaded from " + mDataLocation);
			}
			else
				Debug.Log("No data to load!");
		}
		if(GUI.Button(new Rect(200, 0, 100, 100), "Print Item"))
		{
			Debug.Log(mData.score);
			Debug.Log(mData.name);
			Debug.Log(mData.sampleEnum);
		}
	}
}
